package com.vimi8.ebb.api.geo;


import com.vimi8.ebb.model.GeoCodings;
import com.vimi8.ebb.service.GeoCodingsService;
import com.vimi8.ebb.util.SubStringUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-18T01:39:25.066Z")

@Controller
public class GeoApiController implements GeoApi {

    private static final Logger logger = LoggerFactory.getLogger("FarmApiController");
    @Autowired
    private GeoCodingsService geoCodingsService;

    public ResponseEntity<List<GeoCodings>> getGeoCodingsGET(@ApiParam(value = "地址代码", required = true) @RequestParam(value = "addressCode", required = true) String addressCode

    ) {
        //利用工具类生成带通配符的地址代码
        GeoCodings geoCodings= geoCodingsService.getGeoCodingsById(addressCode);
        int regionLever=0;
        if(geoCodings!=null){
            regionLever=geoCodings.getRegionLever()+1;
        }
        String geoCode = SubStringUtil.separateGeoCode(addressCode, regionLever+"");

        logger.info("======================================code:" + geoCode);
        List<GeoCodings> geoCodingsList = geoCodingsService.getGeoCodingsByCode(addressCode, geoCode);
        return new ResponseEntity<List<GeoCodings>>(geoCodingsList, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<String> getAddressWhole(@ApiParam(value = "地址代码", required = true) @RequestParam(value = "addressCode", required = true) String addressCode) {
        String address = geoCodingsService.getAddressWhole(addressCode);
        return new ResponseEntity<String>(address, HttpStatus.OK);
    }

}
